import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PingTester {
	
	public ConnectionStatus latencyTest(String host) throws IOException, InterruptedException {
		String OS = System.getProperty("os.name");
		
		ConnectionStatus pingStatus = new ConnectionStatus();
		pingStatus.currentLightStatus = ConnectionStatus.lights.RED;
		
		String latencyS;
		double latency;
		
		if (OS.startsWith("Windows")) {
			latencyS = windowsPing(host);
		} else if (OS.startsWith("Linux")) {
			latencyS = unixPing(host,1);
		} else if (OS.startsWith("Mac")) {
			latencyS = unixPing(host,2);
		} else {
			System.out.println("Unknown Operating System");
			latencyS = "Unknown???";
		}
		
		try {
			latency = Double.parseDouble(latencyS);
		} catch (Exception e) {
			latencyS = "???";
		}
		
		pingStatus.latency = latencyS;
		
		return pingStatus;
	}
	
	private static String windowsPing(String host) throws IOException, InterruptedException {
		
		String s1 = "ping ";
		String s3 = " -n 10 -w 1000";
		String cmd = s1 + host + s3;
		System.out.println(cmd);
				
		Runtime r = Runtime.getRuntime();
		Process p = r.exec(cmd);			
		
		BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
		
		String line;
		String lastValidLine = null;
		
		// we only need to extract the last valid line. If the ping was successful, this will contain
		// the average latency. If something went wrong, this will contain gibberish and we'll figure 
		// out the bad response when we try to parseInt something that is not a double
		while ((line = is.readLine()) != null) {
			System.out.println(line);
			lastValidLine = line;
		}
		
		int exitValue = p.waitFor();
		if (exitValue != 0) {
			System.out.println("Windows: Bad Exit Value");
			return "Windows???";
		}
		
		String[] words = lastValidLine.split(" ");
		String finalWord = words[words.length - 1];
		String latencyS = finalWord.substring(0, finalWord.length()-2);
		return latencyS;		
	}
	
	private static String unixPing(String host, int flag) throws IOException, InterruptedException {
		String s1 = "ping";
		String s3 = "-c 10 -s 32";
		String cmd;
		if (flag == 1)
			cmd = s1 + " " + host + " " + s3;
		else
			cmd = s1 + " " + s3 + " " + host;
		
		System.out.println(cmd);
				
		Runtime r = Runtime.getRuntime();
		Process p = r.exec(cmd);			
		
		BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
		
		String line;
		String lastValidLine = null;
						
		// we only need to extract the last valid line. If the ping was successful, this will contain
		// the average latency. If something went wrong, this will contain gibberish and we'll figure 
		// out the bad response when we try to parseInt something that is not a double
		while ((line = is.readLine()) != null) {
			System.out.println(line);
			lastValidLine = line;
		}
		
		int exitValue = p.waitFor();
		if (exitValue != 0) {
			System.out.println("Linux: Bad Exit Value");
			return "Unix???";
		}
		
		String[] words = lastValidLine.split("/");
		if (words.length != 7) {
			System.out.println("Unix: Bad Exit Value");
			return "Unix???";			
		}
		
		String latencyS = words[words.length-3];
		return latencyS;		
	}
} // end of class